<template>
  <div class="add-enterprise">
    <header class="add-header">
      <div class="left">
        <span class="arrow" @click="$router.back()"><i class="el-icon-arrow-left" />返回</span>
        <span>|</span>
        <span>{{ title }}</span>
      </div>
      <div class="right">
        黑马程序员
      </div>
    </header>
    <main class="add-main">
      <div class="form-container">
        <div class="title">企业信息</div>
        <div class="form">
          <el-form ref="ruleForm" label-width="100px" :model="addForm" :rules="rules">
            <el-form-item label="企业名称" prop="name">
              <el-input v-model="addForm.name" />
            </el-form-item>
            <el-form-item label="法人" prop="legalPerson">
              <el-input v-model="addForm.legalPerson" />
            </el-form-item>
            <el-form-item label="注册地址" prop="registeredAddress">
              <el-input v-model="addForm.registeredAddress" />
            </el-form-item>
            <el-form-item label="所在行业" prop="industryCode">
              <el-select v-model="addForm.industryCode">
                <el-option
                  v-for="item in industry"
                  :key="item.industryCode"
                  :value="item.industryCode"
                  :label="item.industryName"
                  list-type="picture"
                />
              </el-select>
            </el-form-item>
            <el-form-item label="企业联系人" prop="contact">
              <el-input v-model="addForm.contact" />
            </el-form-item>
            <el-form-item label="联系电话" prop="contactNumber">
              <el-input v-model="addForm.contactNumber" />
            </el-form-item>
            <el-form-item label="营业执照" prop="businessLicenseId">
              <el-upload
                action="#"
                :http-request="uploadRequest"
                :before-upload="beforeUpload"
                :file-list="fileList"
                list-type="picture"
              >
                <el-button size="small" type="primary">点击上传</el-button>
                <div slot="tip" class="el-upload__tip">支持扩展名：.png .jpg .jpeg，文件大小不得超过5M</div>
              </el-upload>
            </el-form-item>
          </el-form>
        </div>
      </div>
    </main>
    <footer class="add-footer">
      <div class="btn-container">
        <el-button @click="resetBtn">重置</el-button>
        <el-button type="primary" @click="confirmBtn">确定</el-button>
      </div>
    </footer>
  </div>
</template>

<script>
import { uploadAPI } from '@/api/comm'
import { addEnterprise, editEnterprise, getEnterpriseDetail, getIndustryList } from '@/api/enterprise'

export default {
  data() {
    return {
      addForm: {
        name: '', // 企业名称
        legalPerson: '', // 法人
        registeredAddress: '', // 注册地址
        industryCode: '', // 所在行业
        contact: '', // 企业联系人
        contactNumber: '', // 联系人电话
        businessLicenseUrl: '', // 营业执照url
        businessLicenseId: '' // 营业执照id
      },
      fileList: [],
      rules: {
        // 全部字段均为必填项
        name: [
          { required: true, message: '请输入企业名称', trigger: 'blur' }
        ],
        legalPerson: [
          { required: true, message: '请输入法人', trigger: 'blur' }
        ],
        registeredAddress: [
          { required: true, message: '请输入注册地址', trigger: 'blur' }
        ],
        industryCode: [
          { required: true, message: '请选择行业', trigger: 'blur' }
        ],
        contact: [
          { required: true, message: '请输入企业联系人', trigger: 'blur' }
        ],
        contactNumber: [
          { required: true, message: '请输入联系电话', trigger: 'blur' },
          { pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号码', trigger: 'blur' }
        ],
        businessLicenseId: [
          { required: true, message: '请上传营业执照', trigger: 'blur' }
        ]
      },
      industry: [],
      title: '',
      id: ''
    }
  },
  created() {
    this.getIndustry()
    // 赋值
    this.isModel()
    // 回显
    this.getDettail()
  },
  methods: {
    // id判断
    isModel() {
      if (this.$route.query.id) {
        this.id = this.$route.query.id
        this.title = '编辑企业'
      } else {
        this.title = '增加企业'
      }
    },
    async getIndustry() {
      const { data } = await getIndustryList()
      this.industry = data
    },
    async  getDettail() {
      if (!this.id) {
        // 添加
        return
      } else {
        // eslint-disable-next-line no-unused-vars
        const { data: { businessLicenseName, industryName, rent, ...FormData }} = await getEnterpriseDetail(this.id)
        this.addForm = FormData
        this.fileList.push({ name: businessLicenseName, url: FormData.businessLicenseUrl })
      }
    },
    async confirmBtn() {
      await this.$refs.ruleForm.validate()
      if (!this.id) {
        // 添加功能
        await addEnterprise(this.addForm)
      } else {
        // 编辑功能
        await editEnterprise(this.addForm)
      }

      this.$router.push('/park/enterprise')
    },
    // 重置按钮
    resetBtn() {
      this.$refs.ruleForm.resetFields()
    },
    // 上传验证
    beforeUpload(file) {
      // 定义文件类型列表
      const typeList = ['image/jpeg', 'image/jpg', 'image/png']
      //  定义文件大小不得超过5M
      const fileSize = file.size / 1024 / 1024 < 5
      // 必需为 typeList中的类型才行
      const istypeList = typeList.includes(file.type)
      if (!istypeList) {
        this.$message.error('上传头像图片只能是 JPG 格式!')
      }
      if (!fileSize) {
        this.$message.error('上传头像图片大小不能超过 5MB!')
      }
      return istypeList && fileSize
    },
    async uploadRequest(data) {
      const fd = new FormData()
      fd.append('file', data.file)
      fd.append('type', 'businessLicense')
      const { data: { id, url }} = await uploadAPI(fd)
      this.addForm.businessLicenseUrl = url
      this.addForm.businessLicenseId = id
      this.$refs.ruleForm.validateField('businessLicenseId')
    }

  }
}
</script>

<style scoped lang="scss">
.add-enterprise {
    background-color: #f4f6f8;
    height: 100vh;

    .add-header {
        display: flex;
        justify-content: space-between;
        padding: 0 20px;
        height: 64px;
        line-height: 64px;
        font-size: 16px;
        font-weight: bold;
        background-color: #fff;

        .left {
            span {
                margin-right: 4px;
            }

            .arrow {
                cursor: pointer;
            }
        }

        .right {
            text-align: right;
        }
    }

    .add-main {
        background: #f4f6f8;
        padding: 20px 130px;

        .form-container {
            background-color: #fff;

            .title {
                height: 60px;
                line-height: 60px;
                padding-left: 20px;
            }

            .form {
                margin-bottom: 20px;
                padding: 20px 65px 24px;

                .el-form {
                    display: flex;
                    flex-wrap: wrap;

                    .el-form-item {
                        width: 50%;
                    }
                }
            }
        }
    }

    .add-footer {
        position: fixed;
        bottom: 0;
        width: 100%;
        padding: 24px 50px;
        color: #000000d9;
        font-size: 14px;
        background: #fff;
        text-align: center;
    }
}
</style>
